<?php

abstract class Navbar {
    const CATEGORY      = 'category';
    const SUBSECTION    = 'subsection';
    const ITEM          = 'item';
    const DIVIDER       = 'divider';



    static $expanded;
    static $active;

    private static $data = array();
    private static $barId;




    private static function _fillData() {
        if (ADMIN_MODE == ADMINMODES::ADMIN6) {
            $selfdata = &self::$data;
			
            $cat = self::_newCategory( 'lm-statistics', 'Статистика' );
                self::_newItem( $cat, 'lm-last-15',         'charts.php?lm-last-15',        'Посещения 15 дней',    'icon-user' );
                self::_newItem( $cat, 'lm-last-300',        'charts.php?lm-last-300',       'Посещения 300',        'icon-user' );
                self::_newItem( $cat, 'lm-top-10-users',    'charts.php?lm-top-10-users',   'Топ 30 пользователей', 'icon-user' );
                self::_newItem( $cat, 'lm-users-daily',     'charts.php?lm-users-daily',    'Посещения по дням',    'icon-user' );
                self::_newItem( $cat, 'lm-browsers',        'charts.php?lm-browsers',       'Браузеры и устройства','icon-th-list' );
                self::_newItem( $cat, 'lm-queries',         'charts.php?lm-queries',        'Запросы',              'icon-th-list' );
                self::_newItem( $cat, 'lm-users-online',    'charts.php?lm-users-online',   'Пользователи онлайн',  'icon-th-list' );
			
                
                
            $cat = self::_newCategory('lm-monitoring', 'Мониторинг');
                self::_newItem($cat, 'im-nodata',   'monitoring.php?nodata',    'Проверка данных',  'icon-bell');
                self::_newItem($cat, 'im-nofiles',  'monitoring.php?nofiles',   'Проверка файлов',  'icon-folder-open');
                self::_newItem($cat, 'im-nolink',   'monitoring.php?nolink',    'Проверка связи',   'icon-retweet');
                    
                
                
            $cat = self::_newCategory('lm-manage-usr', 'Пользователи');
                self::_newItem($cat, 'usr-groups', 'users.php?groups', 'Группы пользователей',  'icon-list');     
                self::_newItem($cat, 'usr-access', 'users.php?access', 'Пользователи и права доступа',  'icon-list-alt');
                                               
                                        
                                        
            $cat = self::_newCategory('lm-sys', 'Система');
                $ss = self::_newSubsection($cat, 'lm-sys-web', 'WEB');
                    self::_newItem($ss, 'lm-messages',      'messages.php',     'Сообщения',                'icon-comment');
                    self::_newItem($ss, 'lm-conceptions',   'conceptions.php',  'Концепции',                'icon-th-list');   
                    self::_newItem($ss, 'list_reasons',     'reasons.php',      'Редактирование данных',    'icon-question-sign'); 
                    self::_newItem($ss, 'list_vw',          'views.php',        'Отображения',              'icon-book');
                    
                $ss = self::_newSubsection($cat, 'lm-sys-bot', 'Боты');
                    self::_newItem($ss, 'list_ds',      'install.php?list_ds',  'История подключений',  'icon-book');
                    self::_newItem($ss, 'cache_manual', 'cache.php',            'Пересчёт кэша',        'icon-cog');     
                    self::_newItem($ss, 'botconfig',    'botconfig.php',        'Конфигурация ботов',   'icon-wrench'); 
                
                $ss = self::_newSubsection($cat, 'lm-sys-install', 'Установка клиента');
                    self::_newItem($ss, 'create_ds',    'install.php',          'Создание нового подключения',      'icon-plus-sign');
                    self::_newItem($ss, 'download_dst', 'install.php?download', 'Загрузка инсталляционного пакета', 'icon-download-alt');
                    
                    
            $cat = self::_newCategory('lm-internal', 'Служебная информация');
                self::_newItem($cat, 'list_contacts', 'contacts.php', 'Список контактов',  'icon-book');                    
  
                
                
/*

                $ss = self::_newSubsection($cat, 'lm-install-loader', 'Настройка отображения');
                    self::_newItem($ss, 'test1', '#', 'Loader',  'icon-asterisk');
                    self::_newItem($ss, 'test2', '#', 'Loader',  'icon-asterisk');
                    self::_newDivider($ss);
                    self::_newItem($ss, 'test3', '#', 'Loader',  'icon-asterisk');

*/
        }
    }




    static function getVerticalBar($barId='leftmenu') {
        self::$barId = $barId;
        $catContent = '';

        if (!count(self::$data)) self::_fillData ();

        foreach (self::$data as $thing) $catContent .= self::_renderThat ($thing);

        return
<<<__VBAR__
        <div id="{$barId}">
                $catContent
        </div>
__VBAR__;
      /*
<<<__VBAR__
        <div class="accordion" id="{$barId}">
            <div class="accordion-group">
                $catContent
            </div>
        </div>
__VBAR__;*/
    }







    static function getHorisontalBar($showNavigation=true) {
        $params = array(
            'navbuttons' => $showNavigation? includeFile(APP_PAGE_PARTS.'navbar_btn.php') : ''
        );
        return includeFile(APP_PAGE_PARTS.'navbar.php', $params);
    }














    private static function _newCategory($name, $text) {
        $new = array(
            'type'  => self::CATEGORY,
            'name'  => $name,
            'text'  => $text,
            'data'  => array()
        );
        self::$data[] = $new;
        return $name;
    }



    private static function _newSubsection($parent, $name, $text) {
        $new = array(
            'type'  => self::SUBSECTION,
            'name'  => $name,
            'text'  => $text,
            'data'  => array()
        );
        foreach (self::$data as &$cat) {
            if ($cat['name'] == $parent) {
                $cat['data'][] = $new;
                return $name;
            }
        }
        return $name;
    }


    private static function _newItem($parent, $name, $link, $text, $icon='') {
        $new = array(
            'type'  => self::ITEM,
            'name'  => $name,
            'link'  => $link,
            'text'  => $text,
            'icon'  => $icon,
            'data'  => array()
        );

        foreach (self::$data as &$cat) {
            if ($cat['name'] == $parent) {
                $cat['data'][] = $new;
                return;
            }
            foreach ($cat['data'] as &$item) {
                if ($item['type'] == self::SUBSECTION &&  $item['name'] == $parent) {
                    $item['data'][] = $new;
                    return;
                }
            }
        }
    }


    private static function _newDivider($parent) {
        $new = array(
            'type'  => self::DIVIDER,
            'name'  => '',
            'text'  => '',
            'data'  => array()
        );

        foreach (self::$data as &$cat) {
            if ($cat['name'] == $parent) {
                $cat['data'][] = $new;
                return;
            }
            foreach ($cat['data'] as &$item) {
                if ($item['type'] == self::SUBSECTION &&  $item['name'] == $parent) {
                    $item['data'][] = $new;
                    return;
                }
            }
        }
    }









    private static function _renderCategory($cat) {
        $barId = self::$barId;
        extract($cat, EXTR_SKIP);
        $catContent = '';
        //$active =   (self::$expanded == $name)? '' : 'in';
        //$h =   '';//     (self::$expanded == $name)? '' : 'height:0px;';

        foreach ($data as $thing) $catContent .= self::_renderThat ($thing);

        return


        /*
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#$barId" href="#$name">
                $text
            </a>
        </div>

        <div id="$name" class="accordion-body collapse $active" style="$h" >
            <div class="accordion-inner">
                <ul class="nav nav-list">
                    $catContent
                </ul>
            </div>
        </div>

<script>
$(function() {
    $('#$name').collapse('toggle',0);
});
</script>
         */
<<<__CATEGORY__

        <div class="cat-heading">$text</div>
        <div class="cat-content" id="$name">
            <ul class="nav nav-list">
                $catContent
            </ul>
        </div>


__CATEGORY__;

    }



    private static function _renderSubsection($ssec) {
        extract($ssec, EXTR_SKIP);
        $content = '';
        foreach ($data as $thing) $content .= self::_renderThat ($thing);

        return
<<<__SSEC__
        <li class="nav-header" id="$name">$text</li>
        <ul class="nav nav-list" style="margin-bottom:10px;">
            $content
        </ul>
__SSEC__;
    }




    private static function _renderItem($item) {
        extract($item, EXTR_SKIP);
        $active = (self::$active == $name)? 'active' : '';
        $iconwhite = (self::$active == $name)? 'icon-white' : '';
        if ($icon) $icon = "<i class=\"$icon $iconwhite\"></i> ";
        return "<li class=\"{$active}\"><a href=\"{$link}\">{$icon}{$text}</a></li>";

    }

    private static function _renderDivider() {
        return '<li class="divider"></li>';
    }


    private static function _renderThat($item) {
        $type = Arr::getValue($item, 'type', false);
        if (!$type) return '';

        switch ($type) {
            case self::CATEGORY:    return self::_renderCategory($item);
            case self::SUBSECTION:  return self::_renderSubsection($item);
            case self::ITEM:        return self::_renderItem($item);
            case self::DIVIDER:     return self::_renderDivider($item);

            default: return '';
        }

    }




}
?>
